/*
 * main.cpp
 *
 *  Created on: 04/mag/2010
 *      Author: FISSO
 */

#include "main.h"

#define DEFAULT_WIDTH 500
#define DEFAULT_HEIGHT 500

int win;
GLfloat angle = 1.0f;
objLoader *objData;

void drawOrigin( float length ) {
	// draw X [red]
	glColor3f( 1.0f, 0.0f, 0.0f );
	glBegin( GL_LINES);
	glVertex3f( 0, 0, 0 );
	glVertex3f( length, 0, 0 );
	glEnd();
	glRasterPos3f( length, 0.0f, 0.0f );
	glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, 'X' );


	// draw Y [green]
	glColor3f( 0.0f, 1.0f, 0.0f );
	glBegin( GL_LINES );
	glVertex3f( 0, 0, 0 );
	glVertex3f( 0, length, 0 );
	glEnd();
	glRasterPos3f( 0.0f, length, 0.0f );
	glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, 'Y' );


	// draw Z [blue]
	glColor3f( 0.0f, 0.0f, 1.0f );
	glBegin( GL_LINES );
	glVertex3f( 0, 0, 0 );
	glVertex3f( 0, 0, length );
	glEnd();
	glRasterPos3f( 0.0f, 0.0f, length );
	glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, 'Z' );
}

void mainLoop() {
	// Clear color (screen)
	// And depth (used internally to block obstructed objects)
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

	drawOrigin( 0.5f );
	/*
	 glColor3f( 0.84f, 0.53f, 0.8f );
	 glutWireSphere( 0.2f, 20, 20 );
	 glColor3f( 0.0f, 1.0f, 1.0f );
	 glutWireTeapot( 0.5f );
	 */
	glColor3f( 1.0f, 1.0f, 1.0f );

	glPointSize(3.0f);
	glBegin( GL_LINE_STRIP );
		for(int i=0; i<objData->vertexCount; i++) {
			obj_vector* v = objData->vertexList[ i ];
			glVertex3f( v->e[0], v->e[1], v->e[2] );

			glRasterPos3f( v->e[0], v->e[1], v->e[2] );

			char str[5];
			itoa( i, str, 10 );

			glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, str[0] );
		}
	glEnd();

	/*
	glColor3f( 1.0f, 0.0f, 0.0f );
	for(int i=0; i<objData->vertexCount; i++) {
		obj_vector* v = objData->vertexList[ i ];
		glRasterPos3f( v->e[0], v->e[1], v->e[2] );

		char str[5];
		itoa( i, str, 10 );

		glutBitmapCharacter( GLUT_BITMAP_HELVETICA_12, str[0] );
	}
	*/
	glutSwapBuffers();
}

void keyBFunc( unsigned char key, int x, int y ) {
	int i = key-48;
	cout << key << i << endl;
}
void specialFunc( int key, int x, int y ) {
	switch( key ) {
		case GLUT_KEY_LEFT:
			glRotatef( angle, 0.0f, 1.0f, 0.0f );
			break;
		case GLUT_KEY_RIGHT:
			glRotatef( -angle, 0.0f, 1.0f, 0.0f );
			break;
		case GLUT_KEY_DOWN:
			glRotatef( -angle, 1.0f, 0.0f, 0.0f );
			break;
		case GLUT_KEY_UP:
			glRotatef( angle, 1.0f, 0.0f, 0.0f );
			break;
	}
}
void reshapeFunc( int width, int height ) {
	glViewport( 0, 0, width, height );
}

int main( int argc, char** argv ) {

	glutInit( &argc, argv );
	glutInitWindowSize( DEFAULT_WIDTH, DEFAULT_HEIGHT );
	glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE );
	win = glutCreateWindow( "Volox" );

	objData = new objLoader();
	objData->load( "res/base.obj" );

	// Function registration
	glutIdleFunc( mainLoop );
	glutSpecialFunc( specialFunc );
	glutKeyboardFunc( keyBFunc );
	glutReshapeFunc( reshapeFunc );

	glViewport( 0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT );
	glEnable( GL_DEPTH_TEST);
	glScalef( 0.5f, 0.5f, 0.5f );

	// init
	// Load identity matrix ( reset view )
	//	glMatrixMode( GL_PROJECTION);
	//	glLoadIdentity();
	// Load identity matrix ( reset view )
	//	glMatrixMode( GL_MODELVIEW);
	//	glLoadIdentity();


	// main loop
	glutMainLoop();

	delete( objData );

	return EXIT_SUCCESS;
}
